﻿@model PracticeAzure.Website.Models.CorrigendumEditViewModel

@{
    ViewBag.Title = "修改勘误";
}

@section scripts{
    <link rel="stylesheet" type="text/css" href="/Content/cleditor/jquery.cleditor.css" />
    <script type="text/javascript" src="/Content/cleditor/jquery.cleditor.js"></script>

    <script type="text/javascript">
        $(function () {
            // set the html editor for body textarea
            $("#Corrigendum_Body").cleditor({
                width: $(this).parent().width(),
                height: 400
            });
            // remove an attachment upload element
            $(".file-upload-remove").click(function () {
                $(this).parent().remove();
            });
            // add a new attachment upload element
            $("#add-attachment").click(function () {
                var elem = $(".file-upload-group").first().clone(true)
                elem.css("display", "block");
                elem.appendTo("#file-upload-div");
            });
            // add the code block in the body
            $("#add-code").click(function () {
                $("#Body").append("[code][/code]");
            });
            // copy the uplaoded file url
            $(".copy-file-url").click(function () {
                var elem = $(this).parent().children(".file-upload");
                var fullname = elem.val();
                var filename = fullname.substring(fullname.lastIndexOf("\\") + 1);
                if (filename.length > 0) {
                    $("#Body").append("（见附件：" + filename + "。）");
                }
            });
        })
    </script>
}

@using (Html.BeginForm("Edit", "Corrigendum", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ 
    <div>
        @Html.HiddenFor(model => model.Corrigendum.ID)
        
        <div class="editor-label">
            标题：
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Corrigendum.Title, new { style = "width: 100%;" })
            @Html.ValidationMessageFor(model => model.Corrigendum.Title, "!")
        </div>
        
        <div class="editor-label">
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.Corrigendum.Body, new { style = "width: 100%; height: 400px;" })
            @Html.ValidationMessageFor(model => model.Corrigendum.Body, "!")
        </div>
        
        <div class="editor-field">
            <table style="width: 100%;">
                <tr>
                    <th style="width: 70%;">附件地址</th>
                    <th style="width: 20%;">上传日期</th>
                    <th style="width: 10%;">操作</th>
                </tr>
            @foreach(var attachment in Model.ExistingAttachments)
            {
                <tr>
                    <td style="width: 70%;">
                        <a href="@Html.Raw(attachment.Attachment.URL)">@attachment.Attachment.URL</a>
                    </td>
                    <td style="width: 20%;">
                        @attachment.Attachment.CreatedOn.ToString()
                    </td>
                    <td style="width: 10%;">
                        @Html.CheckBox("attachment-" + attachment.Attachment.ID, false) 删除
                        @Html.Hidden("blob-" + attachment.Attachment.ID, System.IO.Path.GetFileName(attachment.Attachment.URL))
                    </td>
                </tr>
            }
            </table>
        </div>

        <div class="editor-label" style="width: 100%; background-color: #aaaaaa;">
            <a href="javascript:void(0)" id="add-attachment">添加附件</a> | 
            <a href="javascript:void(0)" id="add-code">加入代码</a>
        </div>
        <div class="editor-field" id="file-upload-div">
            <div class="file-upload-group" style="display: none;">
                <input type="file" class="file-upload" name="attachement-upload" /> <a href="javascript:void(0)" class="file-upload-remove">删除</a> <a href="javascript:void(0)" class="copy-file-url">复制地址</a>
            </div>
        </div>
        <p>
            <input type="submit" value="保存" />
        </p>
    </div>
}
